package com.fita.admin.service.data.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.support.QueryFilter;
import com.fita.admin.dao.ModelGroupDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.data.ModelGroupService;
import com.fita.entity.data.ModelGroup;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * @author cuteHU
 *
 */
@Service("modelGroupService")
public class ModelGroupServiceImpl extends DataServiceImpl<ModelGroup> implements ModelGroupService {

    @Dao
    private ModelGroupDao modelGroupDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.admin.service.data.ModelGroupService#count()
     */
    public int count() {
        return modelGroupDao.count();
    }

    /* (non-Javadoc)
     * @see com.fita.admin.service.DataServiceImpl#listByPage(java.lang.Class, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<ModelGroup> list(Class<ModelGroup> clazz, Page page) {
        List<ModelGroup> result = genericDaoSupport.searchForList("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE 1 = 1", page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.ModelGroupService#countSearch(java.lang.Class,
     * com.demo2do.core.persistence.support.QueryFilter)
     */
    public int countSearch(Class<ModelGroup> clazz, QueryFilter queryFilter) {
        return genericDaoSupport.searchForInt(
                "SELECT COUNT(*) FROM " + ClassUtils.getShortClassName(clazz) + queryFilter.whereSentence(),
                queryFilter.parameters());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.admin.service.data.ModelGroupService#createModelGroup(com.fita.
     * entity.data.ModelGroup)
     */
    public void createModelGroup(ModelGroup modelGroup) {
        ModelGroup modelGroup1 = modelGroupDao.load(modelGroup.getEnglishName());
        if (modelGroup1 != null) {
            throw BusinessException.newInstance("400", "新增车型组已存在");
        }
        genericDaoSupport.save(modelGroup.initliaze());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.ModelGroupService#freeze(com.fita.entity.data.
     * ModelGroup)
     */
    public void freeze(ModelGroup modelGroup) {
        genericDaoSupport.update(modelGroup.freeze());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.fita.admin.service.data.ModelGroupService#activate(com.fita.entity.data.
     * ModelGroup)
     */
    public void activate(ModelGroup modelGroup) {
        genericDaoSupport.update(modelGroup.activate());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.admin.service.data.ModelGroupService#update(java.lang.Long,
     * com.fita.entity.data.ModelGroup)
     */
    public void update(Long id, ModelGroup modelGroup) {
        List<ModelGroup> loads = modelGroupDao.loads(modelGroup.getEnglishName());
        ModelGroup load = genericDaoSupport.load(ModelGroup.class, id);
        // 如果车型组英文名称没有修改，更新
        if (modelGroup.getEnglishName().equals(load.getEnglishName())) {
            genericDaoSupport.update(load.edit(modelGroup));
        } else if (loads.size() > 0) {
            throw BusinessException.newInstance("400", "车型组英文名称不可重复！");
        }
    }

}
